package com.sphd.restaurantsystem.activitys;

import android.Manifest;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AlertDialog;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import com.baidu.android.pushservice.PushConstants;
import com.baidu.android.pushservice.PushManager;
import com.sphd.restaurantsystem.R;
import com.sphd.restaurantsystem.base.BaseActivity;
import com.sphd.restaurantsystem.utils.AppManager;
import com.sphd.restaurantsystem.utils.updata.UpDataUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import permissions.dispatcher.NeedsPermission;
import permissions.dispatcher.OnNeverAskAgain;
import permissions.dispatcher.OnPermissionDenied;
import permissions.dispatcher.OnShowRationale;
import permissions.dispatcher.PermissionRequest;
import permissions.dispatcher.RuntimePermissions;

@RuntimePermissions
public class StartActivity extends BaseActivity {

    @BindView(R.id.boss_touch)
    ImageView bossTouch;
    @BindView(R.id.appfirest_logon)
    Button appfirestLogon;
    @BindView(R.id.appfirst_register)
    Button appfirstRegister;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_start);
        addActivity(this);
        ButterKnife.bind(this);
        initProgress();
    }

    private void initProgress() {
        StartActivityPermissionsDispatcher.onSuccedWithCheck(this);
    }

    private void checkOk() {
        PushManager.startWork(context,
                PushConstants.LOGIN_TYPE_API_KEY, "kSs880sCXL2eDhu59Os2wrhP");
//                PushConstants.LOGIN_TYPE_API_KEY, "xkjWyMgggcQZGAeBqF1ajfg0");
        UpDataUtils upDataUtils =new UpDataUtils(context,"");
        upDataUtils.checkUpAuto("http://restaurant.zhongxs.com/Restaurant/update/versionManager.xml");

    }

    @OnClick({R.id.boss_touch, R.id.appfirest_logon, R.id.appfirst_register})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.boss_touch:
                break;
            case R.id.appfirest_logon:
                startActivity(LoginActivity.class);
                finishActivity(this);
//                boolean isLogin = (boolean) get(Constant.ISLOGIN, false);
//                boolean isReme = (boolean) get(Constant.ISREMBER, false);
//                if (isLogin && isReme) {
//                    goUser();
//                } else {
//                    startActivity(LoginActivity.class);
//                }
                break;
            case R.id.appfirst_register:
                startActivity(RegisterActivity.class);
                break;
        }
    }
    @NeedsPermission({
            Manifest.permission.ACCESS_COARSE_LOCATION,
            Manifest.permission.READ_PHONE_STATE, Manifest.permission.READ_EXTERNAL_STORAGE})
    void onSucced() {
        checkOk();
    }


    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        StartActivityPermissionsDispatcher.onRequestPermissionsResult(this, requestCode, grantResults);
    }

    @OnShowRationale({
            Manifest.permission.ACCESS_COARSE_LOCATION,
            Manifest.permission.READ_PHONE_STATE, Manifest.permission.READ_EXTERNAL_STORAGE})
    void onCause(final PermissionRequest request) {
        giveMsg(request);
    }

    @OnPermissionDenied({
            Manifest.permission.ACCESS_COARSE_LOCATION,
            Manifest.permission.READ_PHONE_STATE, Manifest.permission.READ_EXTERNAL_STORAGE})
    void onBreak() {
        showHelp();
    }

    @OnNeverAskAgain({
            Manifest.permission.ACCESS_COARSE_LOCATION,
            Manifest.permission.READ_PHONE_STATE, Manifest.permission.READ_EXTERNAL_STORAGE})
    void onNever() {
        showHelp();
    }

    private void giveMsg(PermissionRequest request) {
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setTitle("提示");
        builder.setMessage("应用运行需获取定位、电话信息。请许可授权");
        // 拒绝, 退出应用
        builder.setNegativeButton("算了吧", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
                request.cancel();
                finishActivity(StartActivity.this);
            }
        });

        builder.setPositiveButton("去授权", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
                request.proceed();
            }
        });

        builder.setCancelable(false);
        builder.show();
    }

    private void showHelp() {
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setTitle(R.string.help);
        builder.setMessage(R.string.string_help_text);

        // 拒绝, 退出应用
        builder.setNegativeButton(R.string.quit, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                AppManager.getAppManager().finishActivity(StartActivity.this);
            }
        });

        builder.setPositiveButton(R.string.settings, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                startAppSettings();
            }
        });

        builder.setCancelable(false);

        builder.show();
    }
}
